package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import sangpil.utils.pannel.ChangerDirection;
	import sangpil.utils.pannel.PageChanger;
	import sangpil.utils.pannel.events.PageChangerEvent;
	import sangpil.utils.text.SetFormat;
	import sangpil.utils.text.SetTextField;

	[SWF(width = 760, height = 405, frameRate = 30, background = 0xFFFFFF)]
	public class TestPageChanger extends Sprite
	{
		
		private var currentPage:uint = 1;
		private var totalPage:uint = 1;
		private var pageNumTextField:TextField;
		
		private var pagesArr:Array;
		private var pageChanger:PageChanger;
		private var arrowDirection:int;
		
		public function TestPageChanger()
		{
			trace("Seoul Bongo");
			this.addEventListener(Event.ADDED_TO_STAGE, onStage);
		}
		
		private function onStage(e:Event):void
		{
			//addKeyBoardEvent
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			this.preparation();
			this.setLayout();	
		}
		
		private function preparation():void
		{
			this.pagesArr = [new tempRect01, new tempRect02, new tempRect03, new tempRect04];
			this.totalPage = this.pagesArr.length;
			this.pageChanger = new PageChanger();
			this.pageChanger.addEventListener(PageChangerEvent.MOVING_CHANGED, changed);
		}
		
		private function changed(e:PageChangerEvent):void
		{
			trace("remove page " + ((this.currentPage-1)+this.arrowDirection)); 
			var temp:int = ((this.currentPage-1)+this.arrowDirection);
			this.removeChild(this.pagesArr[temp]);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		}
		
		private function setLayout():void
		{
			//page numbering
			var format:TextFormat = SetFormat.basic();
			this.pageNumTextField = SetTextField.basic(this.currentPage + " / " +this.totalPage, format);
			this.pageNumTextField.x = this.pageNumTextField.y = 3;
			this.addChild(this.pageNumTextField);
			
			//page 1 loaded
			this.addChildAt(this.pagesArr[0], 0);
		}
		
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			//trace(e.keyCode);
			switch(e.keyCode)
			{
				case 39:
					trace("right");
					this.pageControl(ChangerDirection.NEXT_PAGE); 
				break;
				case 37:
					trace("left");
					this.pageControl(ChangerDirection.PREV_PAGE);
				break;
			}
			this.pageNumTextField.text = this.currentPage.toString() + " / " + this.totalPage.toString();
		}
		
		private function pageControl(direction:String):void
		{
			switch(direction)
			{
				case ChangerDirection.NEXT_PAGE:
					if(this.currentPage < this.totalPage)
					{
						this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
						this.arrowDirection = -1;
						this.currentPage += 1;
						this.addChildAt(this.pagesArr[this.currentPage - 1], 0)
						trace(this.pagesArr[this.currentPage - 2].width,this.pagesArr[this.currentPage - 2].height);
						this.pageChanger.changePage(this.pagesArr[this.currentPage - 2], this.pagesArr[this.currentPage - 1], ChangerDirection.FLIP_RIGHT); 
					}
				break;
				case ChangerDirection.PREV_PAGE:
					if(this.currentPage > 1)
					{
						this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
						this.arrowDirection = 1;
						this.currentPage += -1;
						this.addChildAt(this.pagesArr[this.currentPage - 1], 0)
						this.pageChanger.changePage(this.pagesArr[this.currentPage], this.pagesArr[this.currentPage - 1], ChangerDirection.FLIP_LEFT);
					}
				break;
			}
			trace("added " + (this.currentPage - 1));
		}
	}
}
